package com.pureut.order.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.order.dto.SubcontractingOrderDto;
import com.pureut.order.service.SubcontractingOrderService;
import com.pureut.order.vo.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

/***
 * 委外订单控制层
 * Author:C
 * Date:2022/12/02 9:59
 * @DESC
 */
@RestController
@RequestMapping("/subcontracting-order")
public class SubcontractingOrderController extends BaseController {

    @Resource
    SubcontractingOrderService subcontractingOrderService;

    /**
     * 委外订单列表
     */
    @RequiresPermissions("order:subcontracting:order:list")
    @GetMapping("/list")
    public TableDataInfo list(SubcontractingOrderVo subcontractingOrderVo) {
        startPage();
        //查询委外订单列表
        List<SubcontractingOrderDto> list = subcontractingOrderService.getList(subcontractingOrderVo);
        return getDataTable(list);
    }

    /**
     * 新增委外订单
     */
    @RequiresPermissions("order:subcontracting:order:add")
    @Log(title = "新增委外订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SubcontractingOrderVo subcontractingOrderVo) throws Exception {
        return toAjax(subcontractingOrderService.saveSubcontractingOrder(subcontractingOrderVo));
    }

    /**
     * 提交审核
     */
    @RequiresPermissions("order:subcontracting:order:submitReview")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/submitReview/{id}")
    public AjaxResult submitReview(@PathVariable("id") Long id) {
        return toAjax(subcontractingOrderService.submitReview(id));
    }

    /**
     * 上传合同
     */
    @RequiresPermissions("order:subcontracting:order:uploadContract")
    @Log(title = "上传合同", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadContract")
    public AjaxResult uploadContract(@Validated @RequestBody SubcontractingOrderUploadContractVo subcontractingOrderUploadContractVo) {
        return toAjax(subcontractingOrderService.uploadContract(subcontractingOrderUploadContractVo));
    }

    /**
     * 撤销审核
     */
    @RequiresPermissions("order:subcontracting:order:deleteReview")
    @Log(title = "撤销审核", businessType = BusinessType.UPDATE)
    @GetMapping("/deleteReview/{id}")
    public AjaxResult deleteReview(@PathVariable("id") Long id) {
        return toAjax(subcontractingOrderService.deleteReview(id));
    }

    /**
     * 关结委外订单
     */
    @RequiresPermissions("order:subcontracting:order:close")
    @Log(title = "关结委外订单", businessType = BusinessType.UPDATE)
    @PostMapping("/close")
    public AjaxResult close(@RequestBody SubcontractingOrderVo subcontractingOrderVo) {
        return toAjax(subcontractingOrderService.close(subcontractingOrderVo));
    }

    /**
     * 查看
     */
    @RequiresPermissions("order:subcontracting:order:query")
    @GetMapping("/getView/{id}")
    public AjaxResult getView(@PathVariable("id") Long id) {
        return AjaxResult.success(subcontractingOrderService.getView(id));
    }

    /**
     * 修改
     */
    @RequiresPermissions("order:subcontracting:order:edit")
    @Log(title = "修改委外订单", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@Validated @RequestBody SubcontractingOrderUpdateVo subcontractingOrderUpdateVo) throws SQLException {
        return toAjax(subcontractingOrderService.updateSubcontractingOrder(subcontractingOrderUpdateVo));
    }


    /**
     * 订单审核
     */
    @RequiresPermissions("order:subcontracting:order:review")
    @Log(title = "订单审核", businessType = BusinessType.UPDATE)
    @PostMapping("/orderReview")
    public AjaxResult orderReview(@Validated @RequestBody OrderReviewVo orderReviewVo) {
        return toAjax(subcontractingOrderService.orderReview(orderReviewVo));
    }

    /**
     * 合同审核
     */
    @RequiresPermissions("order:subcontracting:order:contractReview")
    @Log(title = "合同审核", businessType = BusinessType.UPDATE)
    @PostMapping("/contractReview")
    public AjaxResult contractReview(@Validated @RequestBody OrderReviewVo orderReviewVo) {
        return toAjax(subcontractingOrderService.contractReview(orderReviewVo));
    }

    /**
     * 删除委外订单
     */
    @RequiresPermissions("order:subcontracting:order:remove")
    @Log(title = "合同审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteSubcontractingOrder/{idStr}")
    public AjaxResult deleteSubcontractingOrder(@PathVariable("idStr") String idStr) {
        return toAjax(subcontractingOrderService.deleteSubcontractingOrder(idStr));
    }

    /**
     * 委外订单获取bom明细列表
     */
    @GetMapping("/detailInventory")
    public AjaxResult getBomDetailList(long bomId) {
        return AjaxResult.success(subcontractingOrderService.getBomDetailList(bomId));
    }

    /**
     * 委外入库回传委外订单（fegin调用）
     */
    @PostMapping("/updateSubcontractingOrderData")
    public boolean updateSubcontractingOrderData(@RequestBody UpdateSubcontractingOrderDataVo updateSubcontractingOrderDataVo) {
        return subcontractingOrderService.updateSubcontractingOrderData(updateSubcontractingOrderDataVo);
    }

    /**
     * 委外入库回传委外订单（撤销 fegin调用）
     */
    @PostMapping("/updateSubcontractingOrderDataDelete")
    public boolean updateSubcontractingOrderDataDelete(@RequestBody UpdateSubcontractingOrderDataVo updateSubcontractingOrderDataVo) {
        return subcontractingOrderService.updateSubcontractingOrderDataDelete(updateSubcontractingOrderDataVo);
    }

    /**
     * 委外退货回传委外订单（fegin退货重量）
     */
    @PostMapping("/updateBackOrderData")
    public boolean updateBackOrderData(@RequestBody UpdateSubcontractingOrderDataVo updateSubcontractingOrderDataVo) {
        return subcontractingOrderService.updateBackOrderData(updateSubcontractingOrderDataVo);
    }


    /**
     * 委外退货回传委外订单（fegin退货重量-减）
     */
    @PostMapping("/updateBackOrderDataDelete")
    public boolean updateBackOrderDataDelete(@RequestBody UpdateSubcontractingOrderDataVo updateSubcontractingOrderDataVo) {
        return subcontractingOrderService.updateBackOrderDataDelete(updateSubcontractingOrderDataVo);
    }

}
